﻿using System.Reflection;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using SBlogCore.Common.App.Manager;
using SBlogCore.Common.Attributes;
using SBlogCore.Common.AuthorizationExtend;
using SBlogCore.Common.CacheExtend;
using SBlogCore.Common.DatabaseAccessor.Extend;
using SBlogCore.Common.Filters;
using SBlogCore.Common.FluentValidatorExtend;
using SBlogCore.Common.Helpers;
using SBlogCore.Common.JWTExtend.Extensions;
using SBlogCore.Common.RemoteRequest.Extensions;
using SBlogCore.Common.SwaggerExtend;
using SBlogCore.Common.MapExtend;
using SBlogCore.Common.FileUploadExtend;
using SBlogCore.EventBus;
using System.Text.Json.Serialization;
using SBlogCore.Common.JsonSerialization.Converters;
using SBlogCore.TaskJob;
using SBlogCore.Common.App;
using SBlogCore.EventBus.CrossProcessEventBus;

namespace SBlogCore.Program
{
    [SkipScan]
    public static class AppServiceRegister
    {
        public static IServiceCollection AddSBlogServices(this IServiceCollection services)
        {
            //string queueName = "SBlogCore.Api";
            //获取所有程序集
            var assembliesEnumerable = ReflectionHelper.GetAllReferencedAssemblies();
            Assembly[] assemblies = assembliesEnumerable as Assembly[] ?? assembliesEnumerable.ToArray();
            AppBase.Assembly = assemblies;
            //自定义Mapster映射关系
            services.AddSblogCoreMap(assemblies);
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            //文件上传工厂
            services.AddScoped<FileUploadFactory>();
            //配置属性绑定
            services.OptionsSetUp();
            //SqlSuger
            services.AddSqlSugerSetUp();
            //EF Core 全局上下文
            services.AddGlobalDbContextSetUp();
            //远程访问
            services.AddRemoteRequest(serve =>
            {
                //可注册多个地址
                serve.AddHttpClient("Test", c => c.BaseAddress = new Uri("https://localhost:7013/"));
            });
            //控制器
            services.AddControllers(option =>
            {
                option.Filters.Add<GlobalActionResultFilter>();
                option.Filters.Add<GlobalExceptionFilter>();
                option.Filters.Add<UnitOfWorkFilter>();
                option.Filters.Add<RequestAsyncActionFilter>();
            }).AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
                options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter("yyyy-MM-dd HH:mm:ss"));
            });
            //过滤器
            services.AddFluentValidatorSetup(assemblies);
            //Swagger
            services.AddEndpointsApiExplorer();
            services.AddSwaggerSetUp();
            //鉴权
            services.AddAuthorizationSetUp(assemblies);
            //JWT
            services.AddAuthenticationJwtSetup();
            //缓存
            services.AddSBlogCatchSetUp();
            //服务内部通信
            services.AddMediatR(assemblies);
            //队列
            services.AddSBlogCoreEventBusSetup(assemblies);
            //定时任务
            services.AddTaskJobSetUp(assemblies);
            return services;
        }
    }
}
